$(function () {
    var queryUrl = '../moduledata/list';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '?Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '模具编码', name: 'moduleCode', index: 'MODULE_CODE', width: 80},
            {label: '模具号', name: 'moduleNo', index: 'MODULE_NO', width: 80},
            {label: '模具类型', name: 'moduleTypeName', index: 'MODULE_TYPE_NAME', width: 80},
            {label: '模具机型', name: 'moduleProduct', index: 'MODULE_PRODUCT', width: 80},
            {label: '档案名称', name: 'fileName', index: 'FILE_NAME', width: 80},
            {label: '创建人', name: 'createUserName', index: 'createUserName', width: 80},
            {
                label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '审批人', name: 'approvalUserName', index: 'approvalUserName', width: 80},
            {
                label: '审批状态',
                name: 'approvalStatus',
                index: 'APPROVAL_STATUS',
                width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: '复审人', name: 'reviewUserName', index: 'reviewUserName', width: 80},
            {
                label: '复审状态', name: 'reviewStatus', index: 'REVIEW_STATUS', width: 80, formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '附件', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.goAnnexFile(\"" + row.moduleCode + "\")'><i class='fa fa-upload'></i>&nbsp;上传</button>";
                }
            }
        ]
    });
    vm.getRoleList();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        isPrivilege: 0,
        showList: true,
        title: null,
        moduleData: {},
        ruleValidate: {
            moduleCode: [
                {required: true, message: '模具编码不能为空', trigger: 'change'}
            ]
        },
        q: {
            moduleNo: ''
        },
        moduleTypeName: ''
    },
    methods: {
        getRoleList: function () {
            Ajax.request({
                url: '../sys/user/isPrivilege',
                async: false,
                successCallback: function (r) {
                    vm.isPrivilege = r.isPrivilege;
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleData = {
                id: '',
                delFlag: 1,
                knowledge: 0,
                approvalStatus: 0,
                reviewStatus: 0,
                moduleCode: '',
                moduleNo: '',
                moduleType: ''
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var result = getSelectedRowData("#jqGrid");
            // 特权人员可以修改审批过的数据
            if(vm.isPrivilege == 0){
                if(result.approvalStatus == "已审核" &&result.reviewStatus == "已审核"){
                    alert("当前审批已经通过，禁止更改！");
                    return;
                }
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.moduleData.id == (null || '') ? "../moduledata/save" : "../moduledata/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleData),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            var result = getSelectedRowData("#jqGrid");
            // 特权人员可以修改审批过的数据
            if(vm.isPrivilege == 0){
                if(result.approvalStatus == "已审核" &&result.reviewStatus == "已审核"){
                    alert("当前审批已经通过，禁止更改！");
                    return;
                }
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                vm.moduleData.delFlag = 0;
                Ajax.request({
                    url: "../moduledata/update",
                    params: JSON.stringify(vm.moduleData),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../moduledata/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../moduledata/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleData = r.moduleData;
                    //获取模具类型
                    Ajax.request({
                        url: "../moduletype/info/" + vm.moduleData.moduleType,
                        async: true,
                        successCallback: function (r) {
                            vm.moduleTypeName = r.moduleType.typeName;
                        }
                    });
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'moduleCode': vm.q.moduleCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                moduleCode: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        review: function () { //审核
            var result = getSelectedRowData("#jqGrid");
            var id = result.id;
            if (id == null) {
                return;
            }
            if (result.approvalStatus === "未审核") {
                vm.getInfo(id);
                confirm('确定要审核选中的记录？', function () {
                    vm.moduleData.approvalStatus = 1;
                    Ajax.request({
                        url: '../moduledata/review',
                        params: JSON.stringify(vm.moduleData),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function (r) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            } else {
                alert('本记录已审核!');
            }
        },
        recheck: function () { //复审
            var result = getSelectedRowData("#jqGrid");
            var id = result.id;
            if (id == null) {
                return;
            }
            if (result.approvalStatus === "未审核") {
                alert('请先审核后复审!');
                return;
            }
            if (result.approvalStatus === "已审核" && result.reviewStatus === "未审核") {
                vm.getInfo(id);
                openWindow({
                    title: "确认复审",
                    area: ['350px', '250px'],
                    content: jQuery("#reviewLayer"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        Ajax.request({
                            url: '../moduledata/recheck',
                            params: {
                                'id': id,
                                'knowledge': vm.moduleData.knowledge,
                                'reviewStatus': vm.moduleData.reviewStatus
                            },
                            type: 'POST',
                            successCallback: function (res) {
                                alert('复审成功!');
                                setTimeout(function () {
                                    location.reload();
                                }, 1000);
                            }
                        });
                    }
                });
            } else {
                alert('本记录已审核!');
            }
        },
        goAnnexFile: function (moduleCode) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-upload"></i> 上传附件',
                content: '../module/moduledatafile.html?moduleCode=' + moduleCode
            })
        },
        getModuleArchives: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择模具',
                content: '../module/moduleArchiveSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getModuleArchives();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        vm.moduleData.moduleCode = res.moduleCode;
                        vm.moduleData.moduleNo = res.moduleNo;
                        vm.moduleData.moduleType = res.moduleType;
                        vm.moduleData.moduleProduct = res.productModel;
                        vm.moduleTypeName = res.moduleTypeName;
                        layer.close(index);
                    }
                }
            })
        }
    }
});
